import javax.swing.JOptionPane;

public class ErrorDetection {
	public static void main (String[] args){
		String input;
		int[] bits;
		String result = "";
		String operation;
		
		do{
			operation = JOptionPane.showInputDialog("Enter '1' for 2D Parity,\n enter '2' for Hamming Code,\n enter '9' to exit: ");
		}while(!(operation.equals("1") || operation.equals("2") && !operation.equals("9")));

		
		//2D Parity Check
		//Will not work with an amount of bits not divisible by 7
		if(operation.equals("1")){
			input = JOptionPane.showInputDialog("Enter bits for 2D Parity Check: ");
			if(input.length() % 7 != 0){
				JOptionPane.showMessageDialog(null,"Bits need to be divisible by 7","Error",JOptionPane.ERROR_MESSAGE);
			}
			//Insert bits into String result and then print
			else{
				bits = TwoDParityCheck.check(input);
				for(int i = 0;i <= bits.length - 1; i++){
					result = result + bits[i];
					if((i + 1) % 8 == 0){
						result = result + "\n";
					}
				}
				JOptionPane.showMessageDialog(null,result,"Bits",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		//Hamming Code
		else if(operation.equals("2")){
			//User inserts bits
			input = JOptionPane.showInputDialog("Enter bits for Hamming Code: ");
			if(input.length() != 7){
				JOptionPane.showMessageDialog(null,"7 bits need to be entered","Error",JOptionPane.ERROR_MESSAGE);
			}
			else{
				//Get bits with hamming code
				bits = HammingCode.check(input);
				for(int i = bits.length - 1;i >= 0;i--){
					result = result + bits[i];
				}
				JOptionPane.showMessageDialog(null, result, "Bits",JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
}
